package com.leetcode.LC;

import java.util.HashSet;

public class LC859 {
    public boolean buddyStrings(String A, String B) {
        //长度不一样不行，不一样的次数必须为2，不一样的字符存入set中，如果set大小为2返回true
        if (A.length() != B.length()) {
            return false;
        }
        if (A.equals(B)){
            HashSet<Character> set = new HashSet<>();
            for (int i = 0; i < A.length(); i++) {
                if (set.contains(A.charAt(i))){
                    return true;
                }else {
                    set.add(A.charAt(i));
                }
            }
            return false;
        }
        int count = 0;
        int[] ints = new int[2];
        int tmp=-1;
//        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < A.length(); i++) {
            if (A.charAt(i) != B.charAt(i)) {
                count++;
                if (count > 2) {
                    return false;
                }
                tmp++;
                ints[tmp]=i;
//                set.add(A.charAt(i));
//                set.add(B.charAt(i));
            }
        }

        if (A.charAt(ints[0])!=B.charAt(ints[1])||A.charAt(ints[1])!=B.charAt(ints[0])){
            return false;
        }else {
            return true;
        }

//        return set.size() == 2||set.size()==0;
    }

    public static void main(String[] args) {
        System.out.println(new LC859().buddyStrings("abca", "abca"));
    }
}
